(function ($) {
	var sourceData;
	var nurseid=''
	var keysArr = [{'key':'RegSciAppDate','type':'date','text':'申请日期'},{'key':'RegSciItemDate','type':'date','text':'立项日期','isMust':'0'},
		{'key':'RegSciType','type':'list','text':'科研类型','isMust':'0'},{'key':'RegSciName','type':'input','text':'项目名称'},
		{'key':'RegSciLead','type':'authType','text':'负责人'},{'key':'RegSciPart','type':'authType','text':'参与人','isMust':'0'},
		{'key':'RegSciSituation','type':'input','text':'完成情况','isMust':'0'}]
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//申请日期^立项日期^科研类型^项目名称^负责人^参与人(多个逗号分隔) ^完成情况^id^状态
	function submitNewData(){
		blurAllInput()
		var status = this.getAttribute('data-id');
		var parr = ''
		for (var i = 0; i < keysArr.length;i++) {
			if (i > 0) {
				parr +='^'
			}
			var oneKey = keysArr[i]
			var type = oneKey['type']
			var id = oneKey['key']
			var dom = $('#'+id)[0];
			var tip = '请选择'
			var str = ''
			if (type == 'authType') { //作者
				str = dom.getAttribute('data-id')
			}else if (type == "input"){
				str = dom.value
				tip = '请输入'
			}else if (type == 'date') {
				str = dom.innerText
			}else{
				str = dom.getAttribute('data-id')
			}
			if (str == '' && oneKey['isMust']!='0') {
				mui.toast(tip+oneKey['text'])
				showEmptyAnimation(id)
				return;
			}
			parr +=str
		}
		parr += '^'
		if (sourceData) {
			parr += sourceData['rw'];
		}
		parr += '^'+status;
		var pdict = {'nurseid':nurseid,'parr':parr}
		var paramDict = {'method':'web.INMSMComm:SaveRegSci','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			console.log(JSON.stringify(result))
			var msg = parseInt(result['msg'])
			if (msg == 0) {
				mui.toast('提交失败')
				return 
			}
			plus.webview.getWebviewById('../WKPlatformPages/ky/keyanList.html').evalJS('detailChanged()')
			mui.back()
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	function updateUI(){
		for (var i = 0; i < keysArr.length;i++) {
			var oneKey = keysArr[i]
			var type = oneKey['type']
			var id = oneKey['key']
			var dom = $('#'+id)[0];
			var valueStr = sourceData[id]==undefined?"":sourceData[id]
			if (type == 'authType') {
				dom.innerText = sourceData[id+'Name']
				dom.setAttribute('data-id',valueStr)
			}else if(type == "input"){
				dom.value = valueStr
			}else if (type == 'date') {
				dom.innerText = valueStr
			}else if (type == 'list'){
				dom.innerText = sourceData[id+'Desc']
				dom.setAttribute('data-id',valueStr)
			}
		}
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		sourceData = self.data
		var perInfo = self.perInfo
		nurseid = perInfo.loginID
		$('#CreatorName')[0].innerText = perInfo.loginName
		$('#applyLoc')[0].innerText = perInfo.loginWardDesc
		if (sourceData) { //修改
			updateUI()
		}
		//统计信息的关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			blurAllInput()
			mui.back()
		})
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		//第1作者 参与者
		mui('body').on('tap','.authType',function(){
			blurAllInput()
			var dom = this
			var type = '多选'
			if (this.id == 'RegSciLead') {
				type = '单选'
			}
			setTimeout(function() {
				toChosePerson(dom.id,type) //防止切换页面时键盘未隐藏
			}, 100);
			
		});
		//下拉列表
		mui('#UL').on('tap','.listType',downListElementTapped); //会议级别 是否本专科 获得奖项
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		window.addEventListener('resize',windowResize)
	}
})(mui)

function selectPerson(domId,params){
	setAuthorDomInfo(domId,params)
}


/*
科研立项
	{"RegSciAppDate":"2022-02-01","RegSciItemDate":"2022-02-08","RegSciType":"33||1","RegSciTypeDesc":"国家自然科学基金",
	"RegSciName":"科研立项测试","RegSciLead":"2","RegSciLeadName":"张芮菲","RegSciPart":"3,4","RegSciPartName":"周渔,程皓",
	"RegSciSituation":"已完成","RegSciStatus":"N","RegSciStatusDesc":"已保存","Creator":"0","CreatorName":"管理员",
	"CreateDate":"2022-02-12","rw":"2"}
*/